/*
 * Copyright (c) 2015 University of Campinas (Unicamp)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
 */

#ifndef OFSWITCH13_LEARNING_CONTROLLER_H
#define OFSWITCH13_LEARNING_CONTROLLER_H

#include "ofswitch13-controller.h"

namespace ns3
{

/**
 * \ingroup ofswitch13
 * \brief An Learning OpenFlow 1.3 controller (works as L2 switch)
 */
class OFSwitch13LearningController : public OFSwitch13Controller
{
  public:
    OFSwitch13LearningController();           //!< Default constructor
    ~OFSwitch13LearningController() override; //!< Dummy destructor.

    /**
     * Register this type.
     * \return The object TypeId.
     */
    static TypeId GetTypeId();

    /** Destructor implementation */
    void DoDispose() override;

    /**
     * Handle packet-in messages sent from switch to this controller. Look for
     * L2 switching information, update the structures and send a packet-out
     * back.
     * \param msg The packet-in message.
     * \param swtch The switch information.
     * \param xid Transaction id.
     * \return 0 if everything's ok, otherwise an error number.
     */
    ofl_err HandlePacketIn(struct ofl_msg_packet_in* msg,
                           Ptr<const RemoteSwitch> swtch,
                           uint32_t xid) override;

    /**
     * Handle flow removed messages sent from switch to this controller. Look
     * for L2 switching information and removes associated entry.
     * \param msg The flow removed message.
     * \param swtch The switch information.
     * \param xid Transaction id.
     * \return 0 if everything's ok, otherwise an error number.
     */
    ofl_err HandleFlowRemoved(struct ofl_msg_flow_removed* msg,
                              Ptr<const RemoteSwitch> swtch,
                              uint32_t xid) override;

  protected:
    // Inherited from OFSwitch13Controller
    void HandshakeSuccessful(Ptr<const RemoteSwitch> swtch) override;

  private:
    /** Map saving <IPv4 address / MAC address> */
    typedef std::map<Ipv4Address, Mac48Address> IpMacMap_t;
    IpMacMap_t m_arpTable; //!< ARP resolution table.

    /**
     * \name L2 switching structures
     */
    //\{
    /** L2SwitchingTable: map MacAddress to port */
    typedef std::map<Mac48Address, uint32_t> L2Table_t;

    /** Map datapathID to L2SwitchingTable */
    typedef std::map<uint64_t, L2Table_t> DatapathMap_t;

    /** Switching information for every datapath */
    DatapathMap_t m_learnedInfo;
    //\}
};

} // namespace ns3
#endif /* OFSWITCH13_LEARNING_CONTROLLER_H */
